package joymouse.logger;

import java.io.PrintStream;

/**
 * A {@link Loggable} that when logged, prints its error code and message and
 * exits with its error code.
 *
 * @author Ben Wolsieffer
 */
public class Error implements Loggable {

    /**
     * Error logged when no joystick can be found.
     */
    public static final Error NO_JOYSTICK_FOUND = new Error(2, "No joystick found");
    /**
     * Error logged when a joystick with the specified name can be found.
     */
    public static final Error INVALID_JOYSTICK_NAME = new Error(1, "Specified joystick not found");
    /**
     * Error logged when a controller in use was disconnected.
     */
    public static final Error CONTROLLER_DISCONNECTED = new Error(3, "Controller disconnected");
    /**
     * Error logged when AWT {@link Robot} creation fails.
     */
    public static final Error CANT_CREATE_ROBOT = new Error(4, "Unable to create Robot");

    /**
     * Prints the error code and message to the specified {@link PrintStream}.
     *
     * @param stream The stream to log to
     */
    @Override
    public void log(PrintStream stream) {
        stream.println("Error " + code + ": " + message);
        System.exit(code);
    }
    private int code;
    private String message;

    /**
     * Creates a new {@code Error} with the specified error code and message.
     *
     * @param code The error code
     * @param message The error message
     */
    protected Error(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * {@inheritDoc} {@code Error}s are log level 2.
     */
    @Override
    public int getLevel() {
        return 2;
    }
}